package com.zjsy.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjsy.po.User;
import com.zjsy.service.UserService;
import com.zjsy.service.impl.UserServiceImpl;

@Controller
public class IndexController {

	
	/**
	 * 登录成功后，后台管理首页
	 */
	@RequestMapping("/")
	public void Index(HttpServletRequest request,HttpServletResponse response) {
		try {
			System.out.println(request.getContextPath()+666);
			response.sendRedirect(request.getContextPath() + "/login");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		return "index";
	}
	
	/**
	 * 用户登录
	 * @return
	 */
	@RequestMapping("/login")
	public String Login(HttpServletRequest request) {
		
		return "login";
	}
	
	/**
	 * 登录校验
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/loginCheck")
	@ResponseBody
	public String loginCheck(Model model,String username,String password,HttpSession session) {
		UserService userService = new UserServiceImpl();
		User user = userService.userLogin(username, password);
		session.setAttribute("user", user);
		model.addAttribute("user",user);
		if(user!=null)return "success";
		else return "error";
		
	}
	
	@RequestMapping("/welcome")
	public String welcome() {
		return "welcome";
	}
}
